package com.gxc.webcore.util;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加解密工具类
 * 
 * @author gxc
 * 
 */
public class EncryptUtil {

	/**
	 * md5消息摘要
	 * 
	 * @param plaintext
	 * @return
	 */
	public static String md5Degest(String plaintext) {
		String ret = null;
		try {
			ret = new String(plaintext);
			MessageDigest md = MessageDigest.getInstance("MD5");
			ret = StringUtil.byteArray2hexString(md.digest(ret.getBytes()));
		} catch (Exception e) {
		}
		return ret;
	}

	/**
	 * RSA 生成密钥对
	 * 
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static KeyPair rsaGenerateKeyPare() {
		try {
			KeyPairGenerator kpg;
			kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(1024);
			return kpg.genKeyPair();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * RSA公钥加密
	 * 
	 * @param plaintext
	 *            明文
	 * @param publicKey
	 *            公钥
	 * @return 十六进制密文
	 * @throws Exception
	 */
	public static String rsaEncrypt(String plaintext, PublicKey publicKey)
			throws Exception {
		if (plaintext == null || publicKey == null) {
			return null;
		}
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] bytes = cipher.doFinal(plaintext.getBytes());
		return StringUtil.byteArray2hexString(bytes);
	}

	/**
	 * RSA私钥解密
	 * 
	 * @param ciphertext
	 *            十六进制密文
	 * @param privateKey
	 *            RSA私钥
	 * @return 明文字符串
	 */
	public static String rsaDecrypt(String ciphertext, PrivateKey privateKey)
			throws Exception {
		if (ciphertext == null || privateKey == null) {
			return null;
		}
		byte[] bytes = StringUtil.hexString2byteArray(ciphertext);
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] resultBytes = cipher.doFinal(bytes);
		return StringUtil.byteArrayToString(resultBytes);
	}

	public static String aesGenerateKey() {
		return StringUtil.getUUID();
	}

	/**
	 * AES加密
	 * 
	 * @param plaintext
	 *            明文
	 * @param encryptKey
	 *            密钥,长度为32的十六进制字符串
	 * @return 十六进制密文
	 */
	public static String aesEncrypt(String plaintext, String key) {
		if (plaintext == null || key == null) {
			return null;
		}
		if (plaintext.equals("") || key.equals("")) {
			return null;
		}
		byte[] keyBytes = StringUtil.hexString2byteArray(key);
		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, keySpec);
			byte[] resultBytes = cipher.doFinal(plaintext.getBytes());
			return StringUtil.byteArray2hexString(resultBytes);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * AES解密
	 * 
	 * @param ciphertext
	 *            十六进制密文
	 * @param decriptKey
	 *            密钥,长度为32的十六进制字符串
	 * @return 明文字符串
	 */
	public static String aesDecrypt(String ciphertext, String key) {
		if (ciphertext == null || key == null) {
			return null;
		}
		if (ciphertext.equals("") || key.equals("")) {
			return null;
		}
		byte[] keyBytes = StringUtil.hexString2byteArray(key);
		byte[] cipherBytes = StringUtil.hexString2byteArray(ciphertext);
		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
		try {
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, keySpec);
			byte[] resultBytes = cipher.doFinal(cipherBytes);
			return StringUtil.byteArrayToString(resultBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
